import threading

import openpyxl
import xlrd

from DataPlatform.settings import STATIC_ROOT
from business.helper import safer
from business.models import LoadParams, EnergyPurchase, EnergySale2C, WindLoad, LightLoad, DesignPlan


class ComputeThread(threading.Thread):
    def __init__(self, config, data_file, light_file, wind_file, design_id, economic_data, who):
        super(ComputeThread, self).__init__()
        self.config, self.data_file, self.light_file, self.wind_file, self.design_id = config, data_file, light_file, wind_file, design_id
        self.who = who
        self.economic_data = economic_data
        self.prc = 0
        self.msg = ''
        # 将结果模板excel拷贝后生成出属于用户本次规划的一份
        target_path = STATIC_ROOT + '/business/params_file/' + 'result_temp.xlsx'
        copy_path = STATIC_ROOT + '/business/user_params_file/' + who + "_result_" + design_id + '.xlsx'
        self.save_path = copy_path
        book = openpyxl.load_workbook(target_path)
        self.target_book = book
        book.save(copy_path)

    def run(self) -> None:
        item_config = DesignPlan.objects.get(design_id=self.design_id)  # 本次计算的配置文件
        data_work_boot = xlrd.open_workbook(self.data_file)
        load_sheet = data_work_boot.sheets()[0]  # 负载页面
        price_sheet = data_work_boot.sheets()[1]  # 购买能源价钱页
        sell_sheet = data_work_boot.sheets()[2]  # 售卖能源价钱页
        opt_count = load_sheet.nrows  # 根据输入的负载规模，计算对应的结果

        # 保存负载文件到临时数据库
        for i in range(1, opt_count):
            LoadParams(
                design_id=self.design_id,
                hour=safer(load_sheet.cell_value(i, 1)),
                head_load=safer(load_sheet.cell_value(i, 2)),
                cold_load=safer(load_sheet.cell_value(i, 3)),
                ele_load=safer(load_sheet.cell_value(i, 4)),
                gas_load=safer(load_sheet.cell_value(i, 5))).save()
        # 保存购买能源价钱
        for i in range(1, price_sheet.nrows):
            EnergyPurchase(
                design_id=self.design_id,
                hour=safer(price_sheet.cell_value(i, 0)),
                power=safer(price_sheet.cell_value(i, 1)),
                gas=safer(price_sheet.cell_value(i, 2)),
                heat=safer(price_sheet.cell_value(i, 3))).save()

        with sell_sheet as sell:
            for i in range(1, sell.nrows):
                EnergySale2C(
                    design_id=self.design_id,
                    hour=safer(sell.cell_value(i, 0)),
                    power=safer(sell.cell_value(i, 1)),
                    heat=safer(sell.cell_value(i, 2)),
                    cold=safer(sell.cell_value(i, 3))).save()

        if self.light_file:
            with xlrd.open_workbook(self.light_file).sheets()[0] as light:
                for i in range(i, light.nrows):
                    LightLoad(
                        design_id=self.design_id,
                        hour=safer(light.cell_value(i, 0)),
                        pv=safer(light.cell_value(i, 1))).save()
        if self.wind_file:
            with xlrd.open_workbook(self.wind_file).wind_file.sheets()[0] as wind:
                for i in range(i, wind.nrows):
                    WindLoad(
                        design_id=self.design_id,
                        hour=safer(wind.cell_value(i, 0)),
                        pv=safer(wind.cell_value(i, 1))).save()
        pass

    def get_result(self):
        # print(self.prc, self.msg)
        return self.prc, self.msg
